package util;

import java.io.IOException;
import java.net.Socket;

/**
 * An {@link MBMessageStreamInterface} for the streams of a socket.
 * @author Bitidork
 *
 */
public class MBMessageSocketInterface extends MBMessageStreamInterface {
	
	// %%%%%%%%%%%%%%%%%%%%%%
	// Constructors
	// %%%%%%%%%%%%%%%%%%%%%%
	
	/**
	 * Creates an {@link MBMessageStreamInterface} around the streams of the supplied socket.
	 * The socket should be connected and open.
	 * @param sock
	 * @throws IOException if an I/O error occurs when creating a stream, the socket is closed, the socket is not connected, or a socket stream has been shutdown
	 */
	public MBMessageSocketInterface( final Socket sock ) throws IOException {
		super( sock.getInputStream(), sock.getOutputStream() );
		
		this.sock = sock;
	}
	
	// %%%%%%%%%%%%%%%%%%%%%%
	// Fields/Accessors
	// %%%%%%%%%%%%%%%%%%%%%%
	
	/**
	 * The socket this interface wraps around.
	 */
	private final Socket sock;
	
	/**
	 * Gets the socket this interface wraps around.
	 * @return The socket this interface wraps around.
	 */
	public final Socket getSocket( ) { 
		return this.sock;
	}
	
	@Override
	public void close( ) {
		try {
			this.sock.close( );
		} catch (Exception e) {
			// best-effort
		}
	}
}
